package main

import (
    "facecat"
    "io/ioutil"
    "net/http" 
    "strings"
)

var gPaint = facecat.CreatePaint()
var room = "1234"
var pwd = "1234"
var lastIndex = -1
var client = &http.Client{} 

func checkTheme(){
 req, err := http.NewRequest("GET", "http://127.0.0.1:9962?func=fcscript&funcname=getChatData('" + room + "','" + pwd + "'," + facecat.ConvertIntToStr(lastIndex)+ ");", nil)  
 if err != nil {  
    return  
 } 
 resp, err := client.Do(req)  
 defer resp.Body.Close()   
 body, err := ioutil.ReadAll(resp.Body)  
 result := string(body)
 if len(result) > 0 && result != "-1,-1"{
     strs := strings.Split(result, ",")
     if gPaint.DefaultUIStyle == "dark"{
         gPaint.DefaultUIStyle = "light"
     }else{
         gPaint.DefaultUIStyle = "dark"
     }
     var pList []*facecat.FCView
     gPaint.Views = pList
     xmlStr, _ := ioutil.ReadFile("xml//mainframe.xml")
    facecat.RenderFaceCat(gPaint, string(xmlStr))
     lastIndex = facecat.ConvertToInt(strs[0])
 }
}

func WndProc(hwnd facecat.HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
    if msg == facecat.WM_TIMER{
		if int(wParam) == 1000{
			checkTheme()
		}
	}
    return facecat.WndProcDefault(gPaint, hwnd, msg, wParam, lParam)
}

func main() {
    facecat.SetMaxWindow()
    facecat.CreateMainWindow(gPaint, "FaceCat-Go", WndProc)
    xmlStr, _ := ioutil.ReadFile("xml//mainframe.xml")
    facecat.RenderFaceCat(gPaint, string(xmlStr))
    facecat.SetTimerW32(gPaint.HWnd, 1000, 100, 0)	
    facecat.ShowWindow(gPaint)
}